//
//  GRRepaymentViewController.m
//  YC_GRice
//
//  Created by luojinrong11 on 2018/7/27.
//  Copyright © 2018年 YeCai. All rights reserved.
//

#import "GRRepaymentViewController.h"
#import "GRAllBillAction.h"
#import "GRPayWayView.h"
@interface GRRepaymentViewController ()<GRPayWayViewDelegate,GRPayManagerDelegate>
@property(nonatomic,assign) CGFloat viewPoint;
@property (weak, nonatomic) IBOutlet UILabel *totalPointLabel;

@property (weak, nonatomic) IBOutlet UILabel *userPointLabel;

@property (weak, nonatomic) IBOutlet UISwitch *viewPointBt;

@property (weak, nonatomic) IBOutlet UITextField *priceTF;

@property (weak, nonatomic) IBOutlet UILabel *payPriceLabel;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topOffset;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;

@end

@implementation GRRepaymentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"还款";
    [self loadData];
    self.timeLabel.text = self.timeStr;
    self.priceTF.text = [NSString stringWithFormat:@"%.2lf",self.price];
    [GRPayManager sharePay].delegate = self;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChange:) name:UITextFieldTextDidChangeNotification object:nil];
}


-(void)textChange:(NSNotification *)notification{

    [self openPointPay:self.viewPointBt];
}


-(void)loadData{
    [GRPersonAction getMyRiceInfomation:^(BOOL success, id responseObject, GRError *error) {
        NSLog(@"%@",responseObject);
        if (success) {
            self.viewPoint = [responseObject[@"viewPoint"] floatValue];
            self.totalPointLabel.text = [NSString stringWithFormat:@"龟米:%.2lf",self.viewPoint];
            [self openPointPay:self.viewPointBt];
        }
    }];
}


- (IBAction)showtipAction:(id)sender {
    [[LFAlertBox defaultAlert]showAlertTitle:@{ @"text":@"还款金额",@"textColor":HEXCOLOR(0x222222) ,@"font":     [UIFont boldSystemFontOfSize:18]} content:@{@"text":@"账单待还金额 + 逾期金额",@"textColor":HEXCOLOR(0x666666) ,@"font":     [UIFont systemFontOfSize:15] } cancleBtn:nil sureBtn:@{@"text":@"知道了",@"textColor":HEXCOLOR(0x02BB00) ,@"font":     [UIFont boldSystemFontOfSize:18] } completion:nil];
}


- (IBAction)submit:(id)sender {
    if (self.viewPoint >= [self.priceTF.text floatValue] && self.viewPointBt.on) {
        [self payAction:1];
    }else{
        [GRPayWayView show:[self.payPriceLabel.text floatValue] Delegate:self];
    }
}

-(void)payOrdertWithPayType:(NSInteger)payType{
    [self payAction:payType];
}

-(void)payAction:(NSInteger)type{
           NSDictionary *dic = @{
                        @"giveType":@(_refunType),
                        @"point": (self.viewPointBt.on ? [NSString stringWithFormat:@"%.2lf",fabsf( [self.userPointLabel.text floatValue])] :@""),
                        @"price":[NSString stringWithFormat:@"%.2lf",[self.payPriceLabel.text floatValue]],
                        @"stagesId":@(_stagesId),
                        @"type":(type == 1? @"point":type == 2?@"alipay":@"wxa")
                          };
    
    [GRAllBillAction applyPay:dic CompleteBlock:^(BOOL success, id responseObject, GRError *error) {
        NSLog(@"%@",responseObject);
        if (success) {
            if (type == 1) {
                NSLog(@"还款成功");
            [self paySuccessAction];
            }else{
                if (type == 2) {
                    [[GRPayManager sharePay] aliPay:responseObject];
                }else{
                    [GRPayManager WeChatPay:responseObject];
                }
            }
        }else{
            [GRHUD showMessage:error.errorMsg];
        }
    }];
    
    
}

-(void)alipayResultDic:(NSDictionary *)resultDic {
    if ([resultDic[@"resultStatus"] isEqual:@"9000"]) {
        NSLog(@"支付成功");
        [self paySuccessAction];
    }else if ([resultDic[@"resultStatus"] isEqual:@"6001"]){
        NSLog(@"已中断支付");
        
    }else{
        NSLog(@"支付失败");
    }
}

-(void)paySuccessAction{
    if(_successBlock != nil){
        self.successBlock();
    }
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)wechatPayResp:(BaseResp *)payResp {
    switch (payResp.errCode){
        case WXSuccess:{
            NSLog(@"支付成功");
            [self paySuccessAction];
            
        }
            break;
        case WXErrCodeUserCancel:{
            NSLog(@"已中断支付");
        }
            break;
        default:{
            NSLog(@"支付失败");
        }
            break;
    }
    NSLog(@"微信支付");
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)openPointPay:(UISwitch *)sender {
    if (sender.on) {
        if (self.viewPoint >= [self.priceTF.text floatValue]) {
            self.userPointLabel.text = [NSString stringWithFormat:@"-%.2lf",[self.priceTF.text floatValue]];
            self.payPriceLabel.text = @"0";
        }else{
            self.userPointLabel.text = [NSString stringWithFormat:@"-%.2lf",self.viewPoint];
            self.payPriceLabel.text = [NSString stringWithFormat:@"-%.2lf",[self.priceTF.text floatValue] - self.viewPoint]  ;
        }
    }else{
        self.payPriceLabel.text = self.priceTF.text;
        self.userPointLabel.text = @"0";
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
